<?php
namespace app\admin\controller;

use app\admin\model\Article as ArticleModel;

class Article extends Base
{
    /**
     * 文章列表
     * @return mixed
     */
    public function index(){
        $where = [];
        //若提交参数
        if ($this->request->isPost()){
            $param = $this->request->param();
            //判断
            if ($param['category_id']){
                $where[] = ['category_id', '=', $param['category_id']];
            }
            if ($param['keywords']){
                $where[] = ['title', 'like', '%'.$param['keywords'].'%'];
            }
            if ($param['start_time']){
                $where[] = ['create_time', '>=', strtotime($param['start_time'])];
            }
            if ($param['end_time']){
                $where[] = ['create_time', '<', strtotime($param['end_time'])];
            }
        }
        //获取列表
        $article = new ArticleModel();
        $list = $article->getDetail($where);

        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 新增文章
     * @return mixed
     */
    public function add(){
        //若提交参数
        if ($this->request->isPost()){
            $param = $this->request->param();
            //获取文件保存后的url
            $saveResult = save_upload_file('cover');
            if ($saveResult['status_code'] == 1){
                $param['cover'] = $saveResult['status_msg'];
            }
            //插入数据
            $article = new ArticleModel();
            $result = $article->allowField(true)->save($param);
            if ($result){
                $this->success('添加成功。');
            }
            $this->error('添加失败！');
        }
        return $this->fetch();
    }

    /**
     * 更新文章信息
     * @return mixed
     */
    public function edit(){
        $param = $this->request->param();
        $where = $this->request->only('id');
        $article = new ArticleModel();
        //若提交参数
        if ($this->request->isPost()){
            //获取文件保存后的url
            $saveResult = save_upload_file('cover');
            if ($saveResult['status_code'] == 1){
                $param['cover'] = $saveResult['status_msg'];
            }
            //更新文章信息
            $result = $article->allowField(true)->save($param, $where);
            if ($result !== false){
                $this->success('操作成功。');
            }
            $this->error('操作失败！');
        }
        //获取文章信息
        $detail = $article->getDetailById($where['id']);

        $this->assign('detail', $detail);
        return $this->fetch();
    }

    /**
     * 删除文章
     */
    public function delete(){
        $param = $this->request->only('id');
        //todo:是否有权限删除
        //硬删除
        $article = new ArticleModel();
        $result = $article->where($param)->delete();
        if ($result){
            $this->success('删除成功。');
        }
        $this->error('删除失败！');
    }

    /**
     * 修改状态
     */
    public function changeStatus(){
        $param = $this->request->only(['id', 'status']);
        //更新状态
        $article = new ArticleModel();
        $result = $article->changeStatus($param['id'], $param['status']);
        if ($result){
            $this->success('更新成功。');
        }
        $this->error('更新失败！');
    }

}